package com.kyrioscraft.cloud.thead.jdklab.tools.reentrant;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author kyrioscraft
 * @Date 2019/2/14 16:05
 * @Description //TODO
 **/
public class FairLock implements Runnable {
    public static ReentrantLock fairLock = new ReentrantLock(true);
    @Override
    public void run() {
        while (true) {
            try{
                fairLock.lock();
                System.out.println(Thread.currentThread().getName() + "获得锁");
            }finally {
                fairLock.unlock();
            }
        }

    }

    public static void main(String[] args) {
        FairLock r1 = new FairLock();
        Thread t1 = new Thread(r1,"thread1");
        Thread t2 = new Thread(r1,"thread2");
        t1.start();
        t2.start();
    }
}
